<template>
  <div class="chart-container">
    <div ref="chartRef" class="chart"></div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue';
import * as echarts from 'echarts';

// 定义图表引用
const chartRef = ref<HTMLDivElement>();

// 示例数据
const chartData = {
  categories: ['产品A', '产品B', '产品C', '产品D', '产品E'],
  currentYear: [320, 302, 341, 374, 390],
  lastYear: [220, 182, 191, 234, 290],
  gateData: [390, 390, 390, 390, 390]
};

let chart: echarts.ECharts | null = null;

onMounted(() => {
  if (!chartRef.value) return;

  // 初始化图表
  chart = echarts.init(chartRef.value);

  // 图表配置
  const option: echarts.EChartsOption = {
    backgroundColor: 'transparent',
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      },
      formatter: (params: any) => {
        const currentData = params[0];
        const lastData = params[1];
        const growth = ((currentData.value - lastData.value) / lastData.value * 100).toFixed(1);
        return `${currentData.name}<br/>
                ${currentData.seriesName}: ${currentData.value}万元<br/>
                ${lastData.seriesName}: ${lastData.value}万元<br/>
                同比增长: ${growth}%`;
      }
    },
    grid: {
      left: '3%',
      right: '25%',
      bottom: '3%',
      top: '6px',
      containLabel: true
    },
    xAxis: {
      type: 'value'
    },
    yAxis: {
      type: 'category',
      data: chartData.categories
    },
    series: [
      {
        name: '同比数据',
        type: 'bar',
        data: chartData.gateData, // 数据全为0，不显示柱子
        itemStyle: {
          color: 'transparent' // 透明柱子
        },
        barWidth: '1px',
        label: {
          show: true,
          position: 'right',
          formatter: (params: any) => {
            console.log('同比的输出', params);
            const index = params.dataIndex;
            return `同比: ${params.value}%`;
          },
          offset: [24, 12] // 文字偏移，避免与其他标签重叠
        }
      },
      {
        name: '今年',
        type: 'bar',
        data: chartData.currentYear,
        itemStyle: {
          color: '#5470c6'
        },
        label: {
          show: true,
          position: 'right',
          formatter: '{c}'
        }
      },
      {
        name: '去年',
        type: 'bar',
        data: chartData.lastYear,
        itemStyle: {
          color: '#91cc75'
        },
        label: {
          show: true,
          position: 'right',
          formatter: '{c}'
        }
      }
    ]
  };

  chart.setOption(option);

  // 响应窗口大小变化
  const handleResize = () => {
    chart?.resize();
  };
  window.addEventListener('resize', handleResize);
});

onUnmounted(() => {
  if (chart) {
    chart.dispose();
  }
  window.removeEventListener('resize', handleResize);
});
</script>

<style scoped>
.chart-container {
  width: 100%;
  height: 400px;
  padding: 20px;
  box-sizing: border-box;
}

.chart {
  width: 100%;
  height: 100%;
  background-color: #f9f9f9;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
</style>
